<?php
require_once dirname(dirname(__FILE__)).'/abstract.php';

class SF_PromoteEvents_TmpUpdatePrice extends Mage_Shell_Abstract
{
    public function autoUpdate()
	{
		$eventCode = $this->getArg("code");
		$action = $this->getArg("action");

		if(!$eventCode){
			Mage::log('更新特价失败，Event Code为空！DEBUG_ERROR');
			exit;
		}
		$eventsData	= Mage::getModel('promoteevents/events')->getCollection()
					  ->addFieldToFilter('event_code',$eventCode)
					  ->getFirstItem();

		if(!$eventsData->getData()) exit;
		
		$fromDate 	= $eventsData['from_date'];
		$toDate   	= $eventsData['to_date'];
		$eventsId 	= $eventsData['id'];
		$eventsProductData = Mage::getModel('promoteevents/product')->getCollection()
							->addFieldToFilter('p_id', $eventsId)
							->addFieldToFilter('status', 1)
							->addFieldToFilter('is_sub_product', 0)
							->getData();
		if(!$eventsProductData) exit;
		
		foreach($eventsProductData as $data)
		{
			$productSku = $data['product_sku'];
			$specialPrice = $data['special_price'];

			if($specialPrice <= 0) continue;
			
			$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSku);
    		if(!$product) continue;
			
			/* action == cancel,则设置special price为空 */
			if($action == 'cancel'){
				$restoreSpecialPrice = $data['restore_special_price'];
				$restoreSpecialPriceFromDate = $data['restore_special_price_from_date'];
				$restoreSpecialPriceToDate = $data['restore_special_price_to_date'];
				$this->restoreData($product,$restoreSpecialPrice,$restoreSpecialPriceFromDate,$restoreSpecialPriceToDate);
			}else{
				$SpecialPrice = $product->getSpecialPrice();
				$SpecialPriceFromDate = $product->getSpecialFromDate();
				$SpecialPriceToDate = $product->getSpecialToDate();
				$this->saveOldSpecialPrice($data['id'],$SpecialPrice,$SpecialPriceFromDate,$SpecialPriceToDate);
				$this->settingData($product,$specialPrice,$fromDate,$toDate);
			}
		}
	}

    public function settingData($product, $price=null, $fromDate=null, $toDate=null)
    {
		/*
		 * 设置特价与最大购买数量(1)
		 */
    	Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
		if($product->getTypeId() == 'simple'){
			$product->setSpecialPrice($price)
				->setSpecialFromDate($fromDate)
				->setSpecialFromDateIsFormated(true)
				->setSpecialToDate($toDate)
				->setSpecialToDateIsFormated(true)
				->setStockData(array(
					'max_sale_qty'=>1,
				))
				->save();
		}
		
		if($product->getTypeId() == 'configurable'){
			$product->setSpecialPrice($price)
				->setSpecialFromDate($fromDate)
				->setSpecialFromDateIsFormated(true)
				->setSpecialToDate($toDate)
				->setSpecialToDateIsFormated(true)
				->save();
			
			$childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());
			foreach($childIds as $ids){
				foreach($ids as $id){
					$childProduct = Mage::getModel('catalog/product')->load($id);
					$childProduct->setStockData(array(
						'max_sale_qty'=>1,
					))
					->save();
				}
			}
		}
    	
    }
    public function restoreData($product, $price=null, $fromDate=null, $toDate=null)
    {
		/*
		 * 还原特价与最大购买数量(10000)
		 */
    	Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
		if($product->getTypeId() == 'simple'){
			$product->setSpecialPrice($price)
				->setSpecialFromDate($fromDate)
				->setSpecialFromDateIsFormated(true)
				->setSpecialToDate($toDate)
				->setSpecialToDateIsFormated(true)
				->setStockData(array(
					'max_sale_qty'=>10000,
					'use_config_max_sale_qty'=>1,
				))
				->save();
		}
		if($product->getTypeId() == 'configurable'){
			$product->setSpecialPrice($price)
				->setSpecialFromDate($fromDate)
				->setSpecialFromDateIsFormated(true)
				->setSpecialToDate($toDate)
				->setSpecialToDateIsFormated(true)
				->save();
			$childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());
			foreach($childIds as $ids){
				foreach($ids as $id){
					$childProduct = Mage::getModel('catalog/product')->load($id);
					$childProduct->setStockData(array(
						'max_sale_qty'=>10000,
						'use_config_max_sale_qty'=>1,
					))
					->save();
				}
			}
		}
    }
	
	public function saveOldSpecialPrice($id,$restoreSpecialPrice=null, $restoreSpecialPriceFromDate=null, $restoreSpecialPriceToDate=null)
    {
		$model = Mage::getModel('promoteevents/product')->load($id);
		if(!$model) return;
    	$model->setData('restore_special_price',$restoreSpecialPrice);
		$model->setData('restore_special_price_from_date',$restoreSpecialPriceFromDate);
		$model->setData('restore_special_price_to_date',$restoreSpecialPriceToDate);
		$model->save();
    }

    public function run()
	{
		$this->autoUpdate();
	}
}


$C = new SF_PromoteEvents_TmpUpdatePrice();
$C->run();
     